package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 添加菜品到购物车
     */
    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        //判断购物车内是否有菜品或者套餐
        QueryWrapper<ShoppingCart> queryWrap = new QueryWrapper<>();
        queryWrap.eq("user_id",BaseContext.getCurrentId());
        queryWrap.eq(shoppingCart.getDishId() != null,"dish_id", shoppingCart.getDishId());
        queryWrap.eq(shoppingCart.getSetmealId()!=null,"setmeal_id", shoppingCart.getSetmealId());

        ShoppingCart cart = shoppingCartService.getOne(queryWrap);
        //判断cart是否为空，为空则添加操作
        if (cart == null) {
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        }else {
            //不为空则统计加一
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        }
        return R.success("添加成功");
    }

    /**
     * 显示购物车内容
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        QueryWrapper<ShoppingCart> queryWrap = new QueryWrapper<>();
        queryWrap.eq("user_id", BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(queryWrap);
        return R.success(list);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("clean")
    public R<String> clean() {
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空完毕");
    }

    /**
     * 购物车数量减少
     */
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        QueryWrapper<ShoppingCart> queryWrap = new QueryWrapper<>();
        queryWrap.eq("user_id",BaseContext.getCurrentId());
        queryWrap.eq(shoppingCart.getDishId() != null,"dish_id", shoppingCart.getDishId());
        queryWrap.eq(shoppingCart.getSetmealId()!=null,"setmeal_id", shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrap);
        Integer number = cart.getNumber();
        //判断当前购物车类容的数量是否大于一，
        if(number==1){
            //等于一则直接删除该数据
            shoppingCartService.removeById(cart.getId());
        }else {
            //大于一则数量减一
            cart.setNumber(cart.getNumber()-1);
            shoppingCartService.updateById(cart);
        }
        return R.success("修改成功");
    }
}
